package com.zyd.shiro.business.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;



import com.zyd.shiro.persistence.dao.MallWxappDao;
import com.zyd.shiro.persistence.beans.Wxapp;

/**
 * 服务层
 * 
 * @author RookieWZW
 *
 */
@Service
public class MallWxappService {

	@Autowired
	private MallWxappDao wxappDao;
	
	

	public List<Wxapp> findAll() {

		return wxappDao.findAll();
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Wxapp> findPage(int page, int size) {
		PageRequest pageRequest = new PageRequest(page-1, size);
		return wxappDao.findAll(pageRequest);
	}

	private Specification<Wxapp> where(Map searchMap) {
		
		return new Specification<Wxapp>() {
          
			@Override
			public Predicate toPredicate(Root<Wxapp> root,CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("app_name")!=null && !"".equals(searchMap.get("app_name"))) {
                	predicateList.add(cb.like(root.get("app_name").as(String.class), "%"+(String)searchMap.get("app_name")+"%"));
                }
                // 
                if (searchMap.get("app_id")!=null && !"".equals(searchMap.get("app_id"))) {
                	predicateList.add(cb.like(root.get("app_id").as(String.class), "%"+(String)searchMap.get("app_id")+"%"));
                }
                // 
                if (searchMap.get("app_secret")!=null && !"".equals(searchMap.get("app_secret"))) {
                	predicateList.add(cb.like(root.get("app_secret").as(String.class), "%"+(String)searchMap.get("app_secret")+"%"));
                }
                // 
                if (searchMap.get("phone_no")!=null && !"".equals(searchMap.get("phone_no"))) {
                	predicateList.add(cb.like(root.get("phone_no").as(String.class), "%"+(String)searchMap.get("phone_no")+"%"));
                }
                // 
                if (searchMap.get("mchid")!=null && !"".equals(searchMap.get("mchid"))) {
                	predicateList.add(cb.like(root.get("mchid").as(String.class), "%"+(String)searchMap.get("mchid")+"%"));
                }
                // 
                if (searchMap.get("apikey")!=null && !"".equals(searchMap.get("apikey"))) {
                	predicateList.add(cb.like(root.get("apikey").as(String.class), "%"+(String)searchMap.get("apikey")+"%"));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
                
            }
        };		
		
	}

	public Page<Wxapp> findSearch(Map whereMap, int page, int size) {
		Specification<Wxapp> specification = where(whereMap);
		PageRequest pageRequest = new PageRequest(page-1, size);
		return wxappDao.findAll(specification, pageRequest);
	}

	public Wxapp findOne(Integer id) {
		return wxappDao.findOne(id);
	}

	public void add(Wxapp wxapp) {
		
		wxappDao.save(wxapp);
	}
	
	public void update(Wxapp wxapp) {
		wxappDao.save(wxapp);
	}

	public void delete(Integer id) {
		wxappDao.delete(id);
	}

	public void deleteList(Integer[] ids) {
		for (Integer id : ids) {
			wxappDao.delete(id);
		}
	}

}
